package com.example.uchain.controller;

import com.example.uchain.demo.Member;
import com.example.uchain.demo.PageBean;
import com.example.uchain.demo.Result;
import com.example.uchain.demo.dto.MemberCountDTO;
import com.example.uchain.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@Slf4j//日志用的注解
@Api(tags = "用户信息")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @GetMapping("/members")
    @ApiOperation("查看所有用户信息")
    public Result page(@RequestParam(defaultValue = "1") Integer Page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String userName , Short sex
                       ){

        log.info("分页查询 参数:{},{},{},{}",Page,pageSize,userName,sex);
        PageBean pageBean=memberService.page(Page,pageSize,userName,sex);
        return Result.success(pageBean);
    }

    @PostMapping("/members")
    @ApiOperation("新增用户信息")
    public Result AddMem(@RequestBody Member member){
        log.info("新增用户(注册添加): {}",member);
        return memberService.AddMem(member);
    }

    @DeleteMapping("/members")
    @ApiOperation("根据id删除用户信息")
    public Result deleteMemById(@RequestParam("id") Integer id){
        log.info("根据id删除用户信息: {}",id);
        return memberService.deleteMemById(id);
    }


    @PutMapping( "/recharge")
    @ApiOperation("修改用户信息")
    public Result updateById(@RequestBody Member member){
        log.info("修改用户信息: {}", member);
        // 1. 执行更新操作并获取结果
        Result updateResult = memberService.updateById(member);

        // 2. 根据结果返回不同响应
        if (updateResult.getCode() == 1) {
            // 成功时返回更新后的数据
            Member updatedMember = memberService.findAll1(member.getId());
            return Result.success(updatedMember);
        } else {
            // 直接返回服务层的错误信息
            return updateResult;
        }
    }

    @GetMapping("/members1")
    @ApiOperation("根据id查看用户信息")
    public Result findAll1(@RequestParam("id") Integer id) {
        log.info("根据id查看用户信息");
        Member memberList = memberService.findAll1(id);
        return Result.success(memberList);
    }
    @GetMapping("/queryMember")
    @ApiOperation("分页查询用户信息")
    public Result pageForMem(@RequestParam(defaultValue = "1") Integer Page,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        String userName,
                        String id ,
                        Integer sign,
                        String phoneNumber,
                        Integer code){

        log.info("分页查询挂牌版权信息 参数:{},{},{},{},{},{},{}",Page,pageSize,userName,id,sign,phoneNumber,code);
        PageBean pageBean=memberService.pageForMem(Page,pageSize,userName,id,sign,phoneNumber,code);
        return Result.success(pageBean);
    }


    //总用户数量和七日内新增用户数量
    @GetMapping("/memberCount")
    @ApiOperation("总用户数量和七日内新增用户数量")
    public Result memberCount(){
        MemberCountDTO counts = memberService.memberCount();
        return Result.success(counts);
    }
}
